package com.petecat.interchan.system.company.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.petecat.interchan.core.constans.Global;
import com.petecat.interchan.core.controller.BaseController;
import com.petecat.interchan.protocol.GlobalHeader;
import com.petecat.interchan.protocol.Result;
import com.petecat.interchan.protocol.base.project.vo.CompanySelectVO;
import com.petecat.interchan.protocol.data.PageVO;
import com.petecat.interchan.protocol.sso.company.dto.SysCompanyAddDTO;
import com.petecat.interchan.protocol.sso.company.dto.SysCompanyQryDTO;
import com.petecat.interchan.protocol.sso.company.dto.SysCompanyUpdDTO;
import com.petecat.interchan.protocol.sso.company.vo.SysCompanyQryVO;
import com.petecat.interchan.system.company.service.ICompanyService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import springfox.documentation.annotations.ApiIgnore;

@Api("公司信息管理")
@RestController
@RequestMapping("/system/company")
public class CompanyController extends BaseController{
	
	@Autowired
	private ICompanyService companyService;
	
	@SuppressWarnings("unchecked")
	@ApiOperation("分页查询公司信息")
	@GetMapping("/queryCompanyByPage")
	public Result<PageVO<SysCompanyQryVO>> queryCompanyByPage(@ModelAttribute SysCompanyQryDTO companyQryDTO,
			@ApiIgnore @RequestHeader(name = Global.GLOBAL_HEADER, required = false) String header) {
		GlobalHeader userInfo = this.getUserInfo(header, true);
		companyQryDTO.setUserId(userInfo.getUserId());
		PageVO<SysCompanyQryVO> pageVO = companyService.queryCompanyByPage(companyQryDTO);
		return (Result<PageVO<SysCompanyQryVO>>) Result.ok(pageVO);
	}
	

	@ApiOperation(value = "新增公司基本信息", notes = "新增")
	@PostMapping(value = "/saveCompany")
	@ApiImplicitParams({ 
			@ApiImplicitParam(name = "sysCompanyAddDTO", value = "公司对象", required = true, paramType = "body", dataType = "SysCompanyAddDTO"), })
	public Result<?> saveCompany(@RequestBody SysCompanyAddDTO sysCompanyAddDTO,
			@ApiIgnore @RequestHeader(name = Global.GLOBAL_HEADER, required = false) String header) {
		GlobalHeader userInfo = this.getUserInfo(header, true);
		companyService.saveCompany(sysCompanyAddDTO, userInfo.getUserId());
		return Result.ok();
	}

	@ApiOperation("删除公司基本信息")
	@DeleteMapping("/removeCompanyById")
	public Result<?> removeCompanyById(@RequestParam String companyId,@RequestParam String state,
			@ApiIgnore @RequestHeader(name = Global.GLOBAL_HEADER, required = false) String header) {
		companyService.deleteCompanyById(companyId,state);
		return Result.ok();
	} 

	@ApiOperation("修改公司基本信息")
	@PutMapping("/modifyCompanyById")
	public Result<?> modifyCompanyById(@RequestBody SysCompanyUpdDTO sysCompanyUpdDTO,
			@ApiIgnore @RequestHeader(name = Global.GLOBAL_HEADER, required = false) String header) {
		GlobalHeader userInfo = this.getUserInfo(header, true);
		companyService.modifyCompanyById(sysCompanyUpdDTO, userInfo.getUserId());
		return Result.ok();
	}
	
	@SuppressWarnings("unchecked")
	@ApiOperation("根据登录用户下拉查询树形公司")
	@GetMapping("/queryCompanySelectTreeByUserId")
	public Result<List<CompanySelectVO>> queryCompanySelectTreeByUserId(@ApiIgnore @RequestHeader(name = Global.GLOBAL_HEADER, required = false) String header) {
		GlobalHeader userInfo = this.getUserInfo(header, true);
		List<CompanySelectVO> selectList = companyService.queryCompanySelectTreeByUserId(userInfo.getUserId());
		return (Result<List<CompanySelectVO>>) Result.ok(selectList);
	}
	
	
}
